package com.bsoft.service.impl;

import com.bsoft.mapper.business.RegisterMapper;
import com.bsoft.mapper.pacs.ReportQueryPacsMapper;
import com.bsoft.service.ReportQueryService;

import com.bsoft.utils.JSONUtil;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author liujx
 * @date 2019/4/26
 */
@Service
public class ReportQueryServiceImpl  implements ReportQueryService {
    private static Logger logger = LoggerFactory.getLogger(ReportQueryServiceImpl.class);
    @Resource
    ReportQueryPacsMapper reportQueryPacsMapper;
    @Resource
    RegisterMapper registerMapper;

    @Override
//    @Transactional(value = "bfTransactionManager", rollbackFor = Exception.class)
    public void getJCBG() {
        String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String beginTiem = nowDate+" "+"00:00:00";
        String endTiem = nowDate+" "+"23:59:59";

        List<Map<String,Object>> jcbgList = reportQueryPacsMapper.getJCBGInfo(beginTiem,endTiem);//从第三方数据库mysql获取视图检查报告信息
        logger.info("获取检查报告长度:"+jcbgList.size());

        for (Map<String, Object> jcbgMap : jcbgList) {

            logger.info("检查报告内容:"+ JSONUtil.map2Json(jcbgMap));
            List<Map<String,Object>> hisJcbgList = registerMapper.getHISJCBGInfo(jcbgMap);//每次先查看HIS是否已经有报告了
            if (hisJcbgList.size()>0){//如果有了就跳过
                continue;
            }

            List<Map<String,Object>> hisJcbgList_update = registerMapper.getHISJCBGInfo_update(jcbgMap);//每次先查看HIS是否已经有报告了
            if (CollectionUtils.isNotEmpty(hisJcbgList_update)){
                hisJcbgList_update.forEach(hisJcbgMap -> {
                    int cout = registerMapper.deleteEmr_Jcbg_Pacs(hisJcbgMap);
                    logger.error("删除报告条数:"+hisJcbgMap.get("SQDH"));
                });
            }

            Map<String,Object> saveJCBGMap = new HashMap<>();
            saveJCBGMap.put("sqdh",jcbgMap.get("sqdh"));
            saveJCBGMap.put("sfzh",jcbgMap.get("sfzh"));
            saveJCBGMap.put("mzhm",jcbgMap.get("mzhm"));
            saveJCBGMap.put("zyhm",jcbgMap.get("zyhm"));
            saveJCBGMap.put("jcjg",jcbgMap.get("jcjg"));
            saveJCBGMap.put("lczd",jcbgMap.get("lczd"));
            saveJCBGMap.put("bgks",jcbgMap.get("bgks"));
            saveJCBGMap.put("bgys",jcbgMap.get("bgys"));
            saveJCBGMap.put("bgrq",jcbgMap.get("bgrq"));
            saveJCBGMap.put("rep_url",jcbgMap.get("rep_url")+"");
            saveJCBGMap.put("img_url",jcbgMap.get("img_url")+"");
            saveJCBGMap.put("img_all_url",jcbgMap.get("Img_all_url")+"");
            try {
                registerMapper.saveJCBG_HIS(saveJCBGMap);
            }catch (Exception e){
                logger.info("报错检查报告失败::sqdh::"+jcbgMap.get("sqdh")+"::"+e);
            }


        }
    }

}